
//
//  MQHeaderView.swift
//  leadpagemoudle
//
//  Created by 许博 on 2017/4/21.
//  Copyright © 2017年 mechat. All rights reserved.
//

import UIKit

class MQToggleView: UIView {

    
    var configModel:MQToggleConfigModel? {
        didSet {
            guard let configModel = configModel else {
                return
            }
            setupUI(configModel: configModel)
            
            guard let subs = configModel.subs else {
                return
            }
            for sub in subs{
                scrollView.addSubview(sub)
            }
        }
    }
    
    
    fileprivate var selectedBtn:UIButton?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor.white
        
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()

        scrollView.frame = CGRect(x: 0, y: 40, width: KScreenW, height: bounds.height - 40)
        maskWindow.frame = CGRect(x: 0, y: 0, width: KScreenW , height: bounds.height - 40)
    }
    
    private func setupUI(configModel:MQToggleConfigModel){
        
        guard let count = configModel.titleArray?.count else {
            return
        }
        scrollView.contentSize = CGSize(width: KScreenW*CGFloat(count), height: bounds.height - 40)
        addSubview(scrollView)
        insertSubview(maskWindow, belowSubview: scrollView)
        var i = CGFloat(0)
        
        guard let titleArray = configModel.titleArray
        else {
            return
        }
        
        for title in titleArray {
            let button = UIButton(type: .custom)
            if count == 1 {
                button.isSelected = true
                selectedBtn = button
                button.frame = CGRect(x: 0, y: 0, width: KScreenW, height: 40)
            }else {
                button.frame = CGRect(x: i * (KScreenW/CGFloat(configModel.arrCount)), y: 0, width: KScreenW/CGFloat(configModel.arrCount), height: 40)
            }
            if i == 0 {
                button.isSelected = true
                selectedBtn = button
            }
            button.backgroundColor = configModel.titleBackColor ?? UIColor.white
            button.setTitle(title, for: .normal)
            button.setTitleColor(configModel.titleColorSelected ?? UIColor.black, for: .selected)
            button.setTitleColor(configModel.titleColorNormal ?? UIColor.steelGrey, for: .normal)
            button.titleLabel?.font = UIFont.systemFont(ofSize: 15)
            button.addTarget(self, action: #selector(toggleBtnClick), for: .touchUpInside)
            button.tag = Int(i)
            i += 1
            addSubview(button)
        }
        if count > 1 {
            addSubview(progressView)
        }
        
        progressView.frame = CGRect(x: 0, y: 38, width: KScreenW/CGFloat(configModel.arrCount), height: 2)
        progressView.backgroundColor = configModel.indicatorColor ??  UIColor.clearBlue

    }
    
    /// 切换指示器
    fileprivate lazy var progressView :UIView = {
        let view = UIView()
        return view
    }()
    
    /// 切换容器
    lazy var scrollView:UIScrollView = {
        let scrollView = UIScrollView()
        
        scrollView.isPagingEnabled = true
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.bounces = false
        scrollView.isScrollEnabled = false
        return scrollView
    }()
    
    /// mask遮罩
    fileprivate lazy var maskWindow:UIView = {
        let view = UIView()
//        view.backgroundColor = UIColor(white: 0, alpha: 0.2)
        let tap = UITapGestureRecognizer(target: self, action: #selector(dismiss))
        tap.delegate = self
        view.addGestureRecognizer(tap)
        return view
        
    }()
    
    @objc func dismiss(){
        print(#function)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 点击切换视图
    @objc private func toggleBtnClick(sender:UIButton){
        selectedBtn?.isSelected = false
        sender.isSelected = true
        selectedBtn = sender
        
        let i = CGFloat(sender.tag)
        guard let count = configModel?.titleArray?.count,
            let animated = configModel?.animated
        else {
            return
        }
        UIView.animate(withDuration: 0.2) {
            self.progressView.frame = CGRect(x:i*KScreenW/CGFloat(count), y: 38, width: KScreenW/CGFloat(count), height: 2)
        }
        scrollView.setContentOffset(CGPoint(x: i*KScreenW, y: 0), animated: animated)
        
    }
   

}



extension MQToggleView:UIGestureRecognizerDelegate {
    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
        if (touch.view?.isEqual(maskWindow))! {
            return true
        }else {
            return false
        }
    }
    
}
